package dk.pun.aot.shared.character;

import java.io.Serializable;

import javax.jdo.annotations.EmbeddedOnly;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

@SuppressWarnings("serial")
@PersistenceCapable @EmbeddedOnly
public class CharacterQualities implements Serializable {

	@Persistent
	private int strength;
	@Persistent
	private int agility;
	@Persistent
	private int conditioning;
	@Persistent
	private int vitality;
	@Persistent
	private int discipline;
	@Persistent
	private int intelligence;
	@Persistent
	private int charisma;
	
	public void setCoreQualityScore(QualityType type, int score) {
		switch (type) {
		case STRENGTH:
			this.strength = score;
			break;
		case AGILITY:
			this.agility = score;
			break;
		case CONDITIONING:
			this.conditioning = score;
			break;
		case VITALITY:
			this.vitality = score;
			break;
		case DISCIPLINE:
			this.discipline = score;
			break;
		case INTELLIGENCE:
			this.intelligence = score;
			break;
		case CHARISMA:
			this.charisma = score;
			break;
		}
	}
	
	public int getQualityScore(QualityType type) {
		int score = 0;
		switch (type) {
		case STRENGTH:
			score = this.strength;
			break;
		case AGILITY:
			score = this.agility;
			break;
		case CONDITIONING:
			score = this.conditioning;
			break;
		case VITALITY:
			score = this.vitality;
			break;
		case DISCIPLINE:
			score = this.discipline;
			break;
		case INTELLIGENCE:
			score = this.intelligence;
			break;
		case CHARISMA:
			score = this.charisma;
			break;
		case ATHLETICISM:
			score = this.getAthleticism();
			break;
		case PHYSICAL_ACUMEN:
			score = this.getPhysicalAcumen();
			break;
		case FITNESS:
			score = this.getFitness();
			break;
		case WILL:
			score = this.getWill();
			break;
		case WISDOM:
			score = this.getWisdom();
			break;
		case WIT:
			score = this.getWit();
			break;
		case PRESENCE:
			score = this.getPresence();
			break;
		case REACTION:
			score = this.getReaction();
			break;
		case PHYSIS:
			score = this.getPhysis();
			break;
		case DEFENSE_RATING:
			score = this.getDefenseRating();
			break;
		}
		return score;
	}

	public int getHealthPool() {
		return this.conditioning + this.vitality;
	}
	
	public int getEndurancePool() {
		return this.conditioning + this.discipline;
	}

	private int getAthleticism() {
		return this.average(this.strength, this.agility);
	}
	
	private int getPhysicalAcumen() {
		return this.average(this.agility, this.conditioning);
	}
	
	private int getFitness() {
		return this.average(this.conditioning, this.vitality);
	}
	
	private int getWill() {
		return this.average(this.vitality, this.discipline);
	}
	
	private int getWisdom() {
		return this.average(this.discipline, this.intelligence);
	}
	
	private int getWit() {
		return this.average(this.intelligence, this.charisma);
	}
	
	private int getPresence() {
		return this.average(this.charisma, this.strength);
	}
	
	private int getReaction() {
		return this.average(this.getAthleticism(), this.getWisdom());
	}
	
	private int getPhysis() {
		int sum = this.strength+this.agility+this.conditioning+this.vitality+this.discipline+this.intelligence+this.charisma;
		int avg = sum/7;
		if (sum%7 > 0) {
			avg++;
		}
		return avg;
	}
	
	private int getDefenseRating() {
		int result = this.getReaction()/2;
		if (this.getReaction()%2 > 0) {
			result++;
		}
		return result;
	}
	
	private int average(int a, int b) {
		int sum = a+b;
		int avg = sum/2;
		if (sum%2 > 0) {
			avg++;
		}
		return avg;
	}
}
